package com.whsxt.controller;

import com.whsxt.domain.Category;
import com.whsxt.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-15 14:22
 * @Description:
 */
@RestController
@RequestMapping("/prod/category")
@Api(tags = "商品分类")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


    @GetMapping("/table")
    @ApiOperation(value = "分页查询商品的分类")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategoryList() {
        List<Category> iPage = categoryService.findCategoryList();
        return ResponseEntity.ok(iPage);
    }


    @GetMapping("/listCategory")
    @ApiOperation(value = "查询商品分类的所有一级节点")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadListCategory() {
        List<Category> iPage = categoryService.findRootCategory();
        return ResponseEntity.ok(iPage);
    }


    @PostMapping("")
    @ApiOperation(value = "新增商品分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<Void> addCategory(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }


    //前台


    @GetMapping("/category/categoryInfo")
    @ApiOperation(value = "根据父节点查询分类")
    public ResponseEntity<List<Category>> categoryInfo(Long parentId) {
        List<Category> iPage = categoryService.findCategoryListById(parentId);
        return ResponseEntity.ok(iPage);
    }


}
